﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;

namespace SMS.dao
{
    public class SemesterDao
    {
        private static readonly SemesterDao INSTANCE = new SemesterDao();

        public static SemesterDao Instance
        {
            get
            {
                return INSTANCE;
            }
        }

        private SemesterDao() { }

        // long 
        public int insert(String name)
        {
            String sql = "insert into semester(sem_name) values (@name)";
            DataFetcher.Instance.setCommand(sql, 1);
            DataFetcher.Instance.addParameter(new SqlParameter("@name", name));
            return DataFetcher.Instance.execute();
        }

        public int update(String name, int semId)
        {
            String sql = "update semester set sem_name = @name where sem_id = @semId";
            DataFetcher.Instance.setCommand(sql, 1);
            DataFetcher.Instance.addParameter(new SqlParameter("@name", name));
            DataFetcher.Instance.addParameter(new SqlParameter("@semId", semId));
            return DataFetcher.Instance.execute();
        }

        public int delete(int semId)
        {
            String sql = "delete from semester where sem_id = @semId";
            DataFetcher.Instance.setCommand(sql, 1);
            DataFetcher.Instance.addParameter(new SqlParameter("@semId", semId));
            return DataFetcher.Instance.execute();
        }

        public DataTable findAll()
        {
            String sql = "select sem_id, sem_name " +
                         "from semester " + 
                         "order by sem_name";
            DataFetcher.Instance.setCommand(sql, 1);
            return DataFetcher.Instance.fetch();
        }

        public ArrayList findById(int id)
        {
            String sql = "select sem_id, sem_name from semester where sem_id = @id";
            DataFetcher.Instance.setCommand(sql, 1);
            DataFetcher.Instance.addParameter(new SqlParameter("@id", id));
            return DataFetcher.Instance.getOneRow();
        }
		public DataTable findsemesterName(string semesterName)
        {
            string sql = "select sem_name from semester where sem_name = @sem_name";
            DataFetcher.Instance.setCommand(sql, 1);
            DataFetcher.Instance.addParameter(new SqlParameter("@sem_name", semesterName));
            return DataFetcher.Instance.fetch();
        }
       

        // end of long
    }
}
